package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;



import com.lastproject.dto.QuestionOptionDTO;
import com.lastproject.ejb.entities.Question;
import com.lastproject.ejb.entities.QuestionOption;

/**
 * Session Bean implementation class QuestionOptionConversion
 */
@Stateless
@LocalBean
public class QuestionOptionConversion {
	@PersistenceContext
	EntityManager em;
    /**
     * Default constructor. 
     */
    public QuestionOptionConversion() {
        // TODO Auto-generated constructor stub
    }
    
    public QuestionOptionDTO fromEntity(QuestionOption qo) {
		QuestionOptionDTO questionOption = new QuestionOptionDTO();
		questionOption.setId(qo.getId());
		questionOption.setText(qo.getText());
		questionOption.setTextDefault(qo.getTextDefault());
		questionOption.setType(qo.getType());
		questionOption.setQuestionId(qo.getQuestion().getId());
		
		return questionOption;
	}

	public QuestionOption fromDTO(QuestionOptionDTO qo) {
		QuestionOption questionOption = em.find(QuestionOption.class, qo.getId());
		if (questionOption == null) {
			questionOption = new QuestionOption();
		}
		questionOption.setId(qo.getId());	
		questionOption.setText(qo.getText());
		questionOption.setTextDefault(qo.getTextDefault());
		questionOption.setType(qo.getType());
		questionOption.setQuestion(em.find(Question.class, qo.getQuestionId()));

		return questionOption;
	}

}
